***********************************************************************************************
* Fig A.6
***********************************************************************************************
u $datadir/subsidy_data, clear 
g n=1
keep id n sub_M jobs_total year 
duplicates drop 
collapse (sum) n sub_M jobs_total, by(year)

g year_string = string(year)
replace year_string=substr(year_string, 3,2)

graph bar n, over(year, label(angle(45))) ytitle("number of subsidies") ylabel(0(5)35)
graph export $appxdir/bar_sub_years.eps, replace

***********************************************************************************************
* Fig A.7
***********************************************************************************************
u $datadir/subsidy_data, clear 
keep id sub_M jobs_total 
duplicates drop 
g ln_sub=log(sub_M)
g ln_jobs = log(jobs_total)

twoway (scatter ln_sub ln_jobs, m(oh) col(black) ///
	text(2.5 3 "R{superscript:2}=.03", place(e) col(red))) ///
   (lfit ln_sub ln_jobs, lpattern(dash)  col(red)), ///
	xtitle("log(total jobs promised)") ///
	ytitle("log(subsidy {c $|}M)") xlabel(3(1)10) ///
	ylabel(2(1)8) legend(off) 
graph export $appxdir/scatter_jobs_ln.eps, replace

*******************************************************************************
* Fig B.1
*******************************************************************************
u $datadir/subsidy_data, clear 
keep id n_bid 
duplicates drop 
hist n_bid, width(1) xlabel(0(5)60) xtitle("Number of Bidders")
graph export $appxdir/n_bidders.eps, replace

*******************************************************************************
* Fig H.1 and H.2 
*******************************************************************************
u $datadir/analysis_cz, clear

keep if ru_limit==1

label var diff_corp_tax "Difference in Corporate Tax (%)"
label var diff_income_tax "Difference in Income Tax (%)"
label var diff_sales_tax "Difference in Sales Tax (%)"
label var diff_proptax "Difference in Property Tax (%)"
label var diff_FHFA "Difference in Housing Price ($1,000)"
label var diff_wage "Difference in Industry Wage ($1,000)"
label var diff_auto_roadnetwork "Difference in Auto Network Density"
label var diff_pr_college "Difference in Population with BA+ (%)"


foreach v in diff_pr_college diff_corp_tax ///
	diff_wage diff_income_tax diff_sales_tax diff_proptax diff_auto_roadnetwork diff_FHFA  {
	hist `v'
	graph export $appxdir/`v'.eps, replace
	}

u $datadir/analysis_cz, clear

keep *_win year
duplicates drop 
rename *_win *

keep corp_tax income_tax proptax auto_roadnetwork FHFA pr_college wage cz statefip year
	
g win_ru=1
	
sa $temp/win, replace 

u $datadir/analysis_cz, clear
drop *_win 
keep if ru_limit==1 

keep corp_tax income_tax proptax auto_roadnetwork FHFA pr_college wage cz statefip year

g win_ru=1
sa $temp/ru, replace

u $datadir/analysis_cz, clear
drop *_win 
drop if ru_limit==1 

keep corp_tax income_tax proptax auto_roadnetwork FHFA pr_college wage cz statefip year

label var corp_tax "Corporate Tax (%)"
label var income_tax "Income Tax (%)"
label var proptax "Property Tax (%)"
label var FHFA "Housing Price ($1,000)"
label var wage "Industry Wage ($1,000)"
label var auto_roadnetwork "Auto Network Density"
label var pr_college "Population with BA+ (%)"

duplicates drop 
g win_ru=0

append using $temp/ru
append using $temp/win 

foreach v in corp_tax income_tax proptax auto_roadnetwork FHFA pr_college wage  {
	twoway (kdensity `v' if win_ru==1, lpattern(dash)) (kdensity `v' if win_ru==0), ///
		legend(order(1 "winners and runner-ups" 2 "all other places")) xtitle("`: var label `v''") ytitle("Density")
	graph export $appxdir/winru_`v'.eps, replace
	}

***********************************************************************************************
*Fig. J.1
***********************************************************************************************
u $datadir/analysis_cz, clear
keep id_win ru_offer jobs_direct invest_B sub_M mult_tot manuf
duplicates drop 
drop if ru_offer==.
sa $temp/ru_offer, replace 

insheet using $datadir/runner_up_correlations_serv.csv, comma clear
sa $temp/s, replace
insheet using $datadir/runner_up_correlations.csv, comma clear
append using $temp/s

rename v2 profits_ru_cond
rename v3 profits_ru
rename v4 v_ru
rename v5 v_ru_cond
rename v6 v_ru_cond_resid
rename v7 v_ru_resid
rename v8 id_win 

merge 1:m id_win using $temp/ru_offer 

reg ru_offer jobs_direct invest_B 
predict r, r

g offer_resid = _b[jobs_direct]*.870  + _b[invest_B]*.900  + r
replace offer_resid =  _b[jobs_direct]*.940 + _b[invest_B]*.250  + r if manuf==0

cor offer_resid profits_ru_cond if manuf==0
cor offer_resid profits_ru_cond if manuf==1 & ru_offer<1000

twoway (scatter offer_resid profits_ru_cond if manuf==0, m(oh) mcol(navy)) ///
(lfit offer_resid profits_ru_cond if manuf==0, lcol(navy)) ///
 (scatter offer_resid profits_ru_cond if manuf==1 & ru_offer<1000, mcol(cranberry) m(dh)) ///
 (lfit offer_resid profits_ru_cond if manuf==1 & ru_offer<1000, lcol(cranberry) lpattern(dash)) if offer_resid!=., ///
 legend(order(1 "trade/services"  3 "manufacturing")) ///
xtitle("runner-up predicted profits ({c $|}M)") ytitle("runner-up subsidy offers ({c $|}M)")
graph export $appxdir/offer_profit.eps, replace 

***********************************************************************************************
* Figure K.1: Observed Subsidies
***********************************************************************************************
u $datadir/analysis_cz, clear
		
keep sub_M id_ manuf 
duplicates drop 
hist sub_M if manuf==1, bin(40) xline(1000, lpattern(dash) lcol(red))
graph export $appxdir/hist_sub_manuf.eps, replace
		
hist sub_M if manuf==0, bin(40) xline(400, lpattern(dash) lcol(red))
graph export $appxdir/hist_sub_serv.eps, replace

***********************************************************************************************
* Figure K.4: Model Fit 
***********************************************************************************************
insheet using $datadir/sim_sub.csv, comma clear 
replace sim_sub=0 if sim_sub<0 //subsidies predicted <0 changed to 0
g manuf=1 
sa $temp/manuf, replace 

insheet using $datadir/sim_sub_serv.csv, comma clear 
replace sim_sub=0 if sim_sub<0 //subsidies predicted <0 changed to 0
g manuf=0

append using $temp/manuf 

twoway (histogram sub_m, bin(50)) (kdensity sim_sub, lcol(red) lw(thick) lpattern(dash)) , legend(order(1 "data" 2 "simulated") col(2)) xtitle("Subsidy ({c $|}M)")
graph export $appxdir/model_fit_whist_all.eps, replace 

preserve 
rename sub_m x

collapse  (mean) mean= x (p25) p25 = x (p10) p10 = x ///
	(p75) p75= x (p50) p50 = x (p90) p90 = x
	
	rename * data_*
	g v1=1
	
sa $temp/data, replace
restore 

rename sim_sub x

collapse  (mean) mean= x (p25) p25 = x (p10) p10 = x ///
	(p75) p75= x (p50) p50 = x (p90) p90 = x
	
	rename * sim_*
	g v1=1
	
merge 1:1 v1 using $temp/data

reshape long sim_ data_ , i(v1) string 

format sim_ data_ %12.2fc

g v_label = _j 
foreach v in 10 25 50 75 90 {
		replace v_label = "`v'$^{th}$" if _j == "p`v'"
}

replace v_label = "Mean" if _j == "mean"
sort v_label 

	* MAKE TABLE
	g tab = "\begin{tabular}{l|rr}" in 1
	g top = "\toprule" in 1
	*Midrule
	g mid = " \midrule" in 1 
	g hline = " \hline" in 1 
	*Bottomrule
	g bot = "\bottomrule" in 1
	*End
	g end = "\end{tabular}" in 1
	*Panel

		g title2 = " Pctile & Data & Simulated \\ " in 1
	g c = " "
	g space =  " \multicolumn{1}{c}{} & & \\ " 

*****Output
	local filename = "modelfit_app"

	listtex tab if _n == 1 using "$appxdir/`filename'.tex", replace rstyle(none)	
		listtex title2 if _n == 1, appendto("$appxdir/`filename'.tex") rstyle(none)	
		listtex hline if _n == 1, appendto("$appxdir/`filename'.tex") rstyle(none)
	listtex v_label data sim  ///
		if _j!="mean", appendto("$appxdir/`filename'.tex") rstyle(tabular) 	
			listtex hline if _n == 1, appendto("$appxdir/`filename'.tex") rstyle(none)
	listtex v_label data sim  ///
		if _j=="mean", appendto("$appxdir/`filename'.tex") rstyle(tabular) 
	listtex hline if _n == 1, appendto("$appxdir/`filename'.tex") rstyle(none)
	listtex space if _n == 1, appendto("$appxdir/`filename'.tex") rstyle(none)
	listtex end if _n == 1, appendto("$appxdir/`filename'.tex") rstyle(none)	 
		
********************************************************************************
* Figure K.5: Heterogeneity in Predict Profits 
********************************************************************************
insheet using $datadir/profits_rc.csv, clear 
g rc=1
sa $temp/rc, replace 

insheet using $datadir/profits_norc.csv, clear 
rename v2 x 
g rc=0 

append using $temp/rc

twoway (kdensity x if rc==1) (kdensity x if rc==0, lcolor(red) lpattern(dash) ) ///
	if inrange(x, -500,1000), ytitle("density") ///
	legend(order(1 "baseline" 2 "without random coefficients")) xtitle("profits ({c $|}M)")
graph export $appxdir/profits_rc.eps, replace


********************************************************************************
* Figure K.6: Optimistic Bidders
********************************************************************************
insheet using $datadir/cf_locations.csv, comma clear 
sa $temp/oo, replace 

insheet using $datadir/cf_locations_serv.csv, comma clear 
append using $temp/oo 
keep if winner==1

keep id_deal mean_v  mean_pi sub_m 
destring sub_m, force replace 
rename sub_m sub_M 

	forvalues i=0/100 {
		g oo`i'= (sub_M >= mean_v*(100-`i')/100)	
		g diff`i' = (sub_M - mean_v*(100-`i')/100)	
		}
		
	collapse (mean) oo* (sum) diff* mean_v mean_pi 
	
	g n=1
reshape long oo diff, i(n mean_*)

g tot_welfare = mean_pi + mean_v*(100-_j)/100
replace tot_welfare = tot_welfare/1000
g welfare_gain = tot_welfare/132.6 - 1 //normalized by total welfare (Table 5) 

g pc_optim = _j/100
label var pc_optim "over-optimism parameter"
label var oo "winner's curse share"
line oo pc_optim, xlabel(0(.1)1) ylabel(0(.1)1) xline(.08 .36, lpattern(dash) lcolor(red)) text(.19 .43 "valuation", color(red)) text(.14 .444 "break-even", color(red)) text(.19 .14 "welfare", color(red)) text(.14 .164 "break-even", color(red))
graph export $appxdir/oo_winnerscurse.eps, replace	
